# -*- coding: utf-8 -*-

"""
Module implementing Dialog_MenuBatch.
"""

import os
from PyQt4.QtCore import pyqtSlot
from PyQt4.QtGui import *

from .Ui_menuBatch import Ui_Dialog_MenuBatch

class Dialog_MenuBatch(QDialog, Ui_Dialog_MenuBatch):
    """
    Class documentation goes here.
    """
    ui = None
    
    def __init__(self, parent = None):
        """
        Constructor
        
        @param parent reference to the parent widget (QWidget)
        """
        QDialog.__init__(self, parent)
        self.currentKey = None
    
    def setUI(self,  ui = None):
        """
        set main windows, or update the table view
        """
        if ui:
            Dialog_MenuBatch.ui = ui     
            
        if Dialog_MenuBatch.ui: #and Dialog_MenuBatch.ui.plugins :
            i = -1
            self.tblMenuBatch.setRowCount(len(Dialog_MenuBatch.ui.plugins))
            for key in Dialog_MenuBatch.ui.plugins:    
                i = i+1
                item = QTableWidgetItem(key)
                self.tblMenuBatch.setItem(i, 0,  item)  
                item = QTableWidgetItem(Dialog_MenuBatch.ui.plugins[key])
                self.tblMenuBatch.setItem(i,  1,  item)
            
        
    @pyqtSlot()
    def on_btnAddMenuBatch_clicked(self):
        """
        Add new plugins to our framework to run
        """
        filename=QFileDialog.getOpenFileName(self, "Add a new plugin", "", "*.py")
        if filename:
           key = os.path.splitext(os.path.basename(filename))[0].upper()
           Dialog_MenuBatch.ui.plugins[key] = filename
           self.setUI()
           #print("key: %s, filename: %s"%(key, filename))
    
    @pyqtSlot()
    def on_btnDelMenuBatch_clicked(self):
        """
        delete current on-focus row in plugins
        """
        if self.currentKey:
            del Dialog_MenuBatch.ui.plugins[self.currentKey]
            self.setUI()
        
    @pyqtSlot()
    def on_okButton_clicked(self):
        """
        Slot documentation goes here.
        """
        # TODO: not implemented yet
        #raise NotImplementedError
    
    @pyqtSlot(QTableWidgetItem)
    def on_tblMenuBatch_itemClicked(self, item):
        """
        Get current focus item, prepare for delete action
        """
        row = self.tblMenuBatch.currentRow()
        item = self.tblMenuBatch.item(row,  0)
        self.currentKey = item.text()
        
